<template>
  <div class="nav-bar">
    <template v-for="(item, index) in menus" :key="item.label">
      <div
        :class="['nav-bar-item', { active: currentIndex === index }]"
        @click="handleClickToPage(item.path, index)"
      >
        {{ item.label }}
      </div>
    </template>
  </div>
</template>

<script>
import { ref, watch } from "vue";
import { useRouter, useRoute } from "vue-router";
import { routesConfig } from "../../router/storge";

export default {
  name: "NavBar",
  setup() {
    const router = useRouter();
    const route = useRoute();
    const currentIndex = ref(0);
    const menus = routesConfig;

    const handleClickToPage = (path, index) => {
      currentIndex.value = index;
      router.push(path);
    };

    watch(
      () => route,
      (newVal) => {
        currentIndex.value = menus.findIndex(
          (item) => newVal.path.indexOf(item.path) !== -1
        );
      },
      { immediate: true, deep: true }
    );

    return {
      menus,
      currentIndex,
      handleClickToPage,
    };
  },
};
</script>

<style scoped>
.nav-bar {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: rgb(80, 97, 124);
}

.nav-bar-item {
  line-height: 50px;
  text-align: center;
}

.active {
  background-color: rgb(108, 132, 171);
}
</style>
